package com.tbi.graphcolouring.io;

import java.util.ArrayList;
import java.util.List;

import com.tbi.graphcolouring.Edge;

public class ColParser {
	private int graphSize;
	private int edgesNumber;
	private List<Edge> edges;
	private List<String> comments;
	
	public ColParser(List<String> lines) {
		edges = new ArrayList<>();
		comments = new ArrayList<>();
		parse(lines);
	}
	
	public int getGraphSize() {
		return graphSize;
	}
	
	public int getEdgesNumber() {
		return edgesNumber;
	}
	
	public List<Edge> getVertices() {
		return edges;
	}
	
	public List<String> getComments() {
		return comments;
	}

	private void parse(List<String> lines) {
		for(String line : lines){
			if(line.startsWith("c")){		//comment
				comments.add(line);
			}else if(line.startsWith("p")){	//graph size
				parseGraphSize(line);
			}else if(line.startsWith("e")){ //vertices
				parseVertex(line);
			}
		}
	}
	
	private void parseGraphSize(String line) {
		String[] words = line.split(" ");
		graphSize = Integer.parseInt(words[2]);
		edgesNumber = Integer.parseInt(words[3]);
	}

	private void parseVertex(String line) {
		String[] words = line.split(" ");
		Edge v = new Edge();
		v.from = Integer.parseInt(words[1]);
		v.to = Integer.parseInt(words[2]);
		if(v.from != v.to){
			edges.add(v);
		}
	}
}
